<?php

use app\assets\ApiAsset;
use app\assets\LayerAsset;
use app\assets\TableAsset;
use app\models\KeyMap;
use app\models\UserCardRecharge;
use app\widgets\LinkPager;
use app\widgets\ManagerTableOp;
use yii\helpers\Html;
use yii\helpers\Url;

/**
 * @var $this yii\web\View
 * @var $rechargeList app\models\UserCardRecharge[]
 * @var $pagination yii\data\Pagination
 */

ApiAsset::register($this);
LayerAsset::register($this);
TableAsset::register($this);

$this->title = '用户卡充值列表';
$this->params['breadcrumbs'][] = '财务管理';
$this->params['breadcrumbs'][] = $this->title;
?>
<?php echo Html::beginForm('?', 'get', ['class'=>'form-inline']);?>
<?php echo Html::endForm();?>
<table class="table table-striped table-bordered table-hover">
    <thead>
    <tr>
        <th class="center">
            <label class="pos-rel">
                <input type="checkbox" class="ace" />
                <span class="lbl"></span>
            </label>
        </th>
        <th>用户名</th>
        <th>代理商</th>
        <th>卡</th>
        <th>充值时长</th>
        <th>价格</th>
        <th>金额</th>
        <th>支付信息</th>
        <th>状态</th>
        <th>创建时间</th>
        <th>支付时间</th>
        <th>卡充值时间</th>
        <th>操作</th>
    </tr>
    </thead>

    <tbody>
    <?php foreach ($rechargeList as $recharge) {?>
        <tr>
            <td class="center"><label class="pos-rel"><input type="checkbox" class="ace" value="<?php echo $recharge->id;?>" /><span class="lbl"><?php echo $recharge->id;?></span></label></td>
            <td><?php echo Html::encode($recharge->user->username);?></td>
            <td><?php if (!empty($recharge->user->agent_id)) {echo '----';} else {
                    echo Html::encode($recharge->user->agent->username);
                }?></td>
            <td><?php echo $recharge->card->iccid;?></td>
            <td><?php echo $recharge->term_length, '（', KeyMap::getValue('card_term_length_unit', $recharge->term_length_unit), '）';?></td>
            <td><?php echo $recharge->price;?></td>
            <td><?php echo $recharge->money;?></td>
            <td><?php echo Html::encode($recharge->trade_no), '<br />', Html::encode($recharge->wx_app_id), '<br />', Html::encode($recharge->wx_open_id);?></td>
            <td><?php echo KeyMap::getValue('user_card_recharge_status', $recharge->status);?></td>
            <td><?php echo Yii::$app->formatter->asDatetime($recharge->create_time);?></td>
            <td><?php echo Yii::$app->formatter->asDatetime($recharge->pay_time);?></td>
            <td><?php echo Yii::$app->formatter->asDatetime($recharge->update_time);?></td>
            <td><?php echo ManagerTableOp::widget(['items' => [
                    !($recharge->status == UserCardRecharge::STATUS_PAID) ?: ['icon' => 'fa fa-check', 'onclick' => 'recharge(' . $recharge->id . ')', 'btn_class' => 'btn btn-xs btn-success', 'tip' => '卡充值'],
                ]]);?></td>
        </tr>
    <?php }?>
    </tbody>
</table>
<?php echo LinkPager::widget(['pagination' => $pagination]);?>
<script>
    /**
     * 给卡充值（增加时长）
     * @param id 充值编号
     */
    function recharge(id) {
        $.get('<?php echo Url::to(['/admin/finance/user-card-recharge-confirm']);?>', {id: id}, function (json) {
            if (callback(json)) {
                layer.msg('卡时长已更新。');
            }
        });
    }
</script>
